import java.util.concurrent.CountDownLatch;

public class Demo2 {
    //CountDownLatch
    public static void main(String[] args) throws InterruptedException {
        //等待10个任务结束
        CountDownLatch latch = new CountDownLatch(10);
        for (int i = 0; i < 10; i++) {
            Thread t = new Thread(()-> {
                try {
                    Thread.sleep(3000);
                    System.out.println("到达终点");
                    //线程任务结束执行countDown 任务减一
                    latch.countDown();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            t.start();
        }
        //等待所有线程结束不然处于阻塞(任务数归为0则结束)
        //所有线程结束才会执行await下面的代码逻辑
        latch.await();

        System.out.println("比赛结束");
    }
}
